﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.ServiceModel;
using System.Text;
using CoreEx.Services;
using LinFu.IoC;
using LinFu.IoC.Configuration;
using LinFu.IoC.Interfaces;

namespace CoreEx.ServiceModel.Tests
{
    public class ServiceBaseFixture : BaseFixture
    {

        private IServiceContainer _serviceContainer;
        
        protected ServiceHost ServiceHost { get; set; }

        public override void SetUp()
        {
            base.SetUp();   
            var directory = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
            var containerLoader = new ContainerLoader();
            _serviceContainer = new ServiceContainer();
            _serviceContainer.LoadFrom(directory,"*.*", containerLoader);
            _serviceContainer.AddService(typeof (ISampleService), typeof (SampleService), LifecycleType.OncePerRequest);
            

            var httpUri = new Uri(string.Format("http://127.0.0.1:6000/{0}.svc", typeof(ISampleService).FullName));            
            var netTcpUri = new Uri(string.Format("net.tcp://127.0.0.1:6001/{0}.svc", typeof(ISampleService).FullName));            
            ServiceHost = _serviceContainer.GetService<ServiceHost>(new object[] { typeof(ISampleService), new [] { netTcpUri,httpUri} });
            ServiceHost.Open();
        }

        public override void TearDown()
        {
            _serviceContainer = null;
            ServiceHost.Close();
            base.TearDown();
        }
    }
}
